package com.zrwl.poscloud.core.restapi.admin.baseInfo;

import com.alibaba.fastjson.JSONArray;
import com.zrwl.poscloud.core.annotion.AuthorityVerify.AuthorityVerify;
import com.zrwl.poscloud.core.annotion.OperationLogger.OperationLogger;
import com.zrwl.poscloud.base.entity.ServerSyncEmployeeInfo;
import com.zrwl.poscloud.commons.exception.ThrowableUtils;
import com.zrwl.poscloud.commons.utils.DeptUtil;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.validator.group.Delete;
import com.zrwl.poscloud.commons.validator.group.GetList;
import com.zrwl.poscloud.commons.validator.group.Insert;
import com.zrwl.poscloud.commons.validator.group.Update;
import com.zrwl.poscloud.xo.service.AdminService;
import com.zrwl.poscloud.xo.service.DeptAdminRService;
import com.zrwl.poscloud.xo.service.UserEmployeeRService;
import com.zrwl.poscloud.xo.service.UserService;
import com.zrwl.poscloud.xo.vo.AdminVO;
import com.zrwl.poscloud.xo.vo.UserEmployeeRVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户关注持卡人员 controller api类
 *
 * @author 刘思童 AutoGenerate
 * @date 2021-02-24
 */
@RestController
@Api(value = "用户关注持卡人员相关接口", tags = {"用户关注持卡人员相关接口"})
@RequestMapping("/userEmployeeR")
@Slf4j
public class UserEmployeeRRestApi {
    @Autowired
    private UserEmployeeRService userEmployeeRService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private DeptAdminRService deptAdminRService;
    @Autowired
    private UserService userService;

    @AuthorityVerify
    @ApiOperation(value = "获取用户关注持卡人员列表", notes = "获取用户关注持卡人员列表", response = String.class)
    @PostMapping("/getList")
    public String getList(@Validated({GetList.class}) @RequestBody UserEmployeeRVO userEmployeeRVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取用户关注持卡人员列表: {}", userEmployeeRVO);
        return ResultUtil.successWithData(userEmployeeRService.getPageList(userEmployeeRVO));
    }

    @AuthorityVerify
    @ApiOperation(value = "获取用户关注持卡人员列表", notes = "获取用户关注持卡人员列表", response = String.class)
    @PostMapping("/getUserEmployeeRByEmployeeCode")
    public String getUserEmployeeRByEmployeeCode(@Validated({GetList.class}) @RequestBody UserEmployeeRVO userEmployeeRVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取用户关注持卡人员列表: {}", userEmployeeRVO);
        if (userEmployeeRVO.getCompanyUid() == null) {
            AdminVO adminVO = adminService.getMe();
            if (!adminVO.isCompany()) {
                return ResultUtil.errorWithMessage("仅商户用户支持此功能");
            }
            userEmployeeRVO.setCompanyUid(adminVO.getCompanyUid());
            if(userEmployeeRVO.getCompanyCode() == null){
                userEmployeeRVO.setCompanyCode(DeptUtil.getLatestCompnay(adminVO.getCompanyCode()));
            }
            userEmployeeRVO.setDeptRoleTree(deptAdminRService.getMyDeptRoleTreeCode());
        }
        return ResultUtil.successWithData(userEmployeeRService.getUserEmployeeRByEmployeeCode(userEmployeeRVO));
    }

    @AuthorityVerify
    @ApiOperation(value = "获取用户关注持卡人员列表", notes = "获取用户关注持卡人员列表", response = String.class)
    @PostMapping("/getAllList")
    public String getAllList(@Validated({GetList.class}) @RequestBody UserEmployeeRVO userEmployeeRVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取用户关注持卡人员列表: {}", userEmployeeRVO);
        return ResultUtil.successWithData(userEmployeeRService.getAllList(userEmployeeRVO));
    }

    @AuthorityVerify
    @OperationLogger(value = "新增用户关注持卡人员")
    @ApiOperation(value = "新增用户关注持卡人员", notes = "新增用户关注持卡人员", response = String.class)
    @PostMapping("/add")
    public String add(@Validated({Insert.class}) @RequestBody UserEmployeeRVO userEmployeeRVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("新增用户关注持卡人员: {}", userEmployeeRVO);
        return userEmployeeRService.add(userEmployeeRVO);
    }

    @AuthorityVerify
    @OperationLogger(value = "修改用户关注持卡人员")
    @ApiOperation(value = "修改用户关注持卡人员", notes = "修改用户关注持卡人员", response = String.class)
    @PostMapping("/edit")
    public String edit(@Validated({Update.class}) @RequestBody UserEmployeeRVO userEmployeeRVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("修改用户关注持卡人员: {}", userEmployeeRVO);
        return userEmployeeRService.edit(userEmployeeRVO);
    }

    @AuthorityVerify
    @OperationLogger(value = "删除用户关注持卡人员")
    @ApiOperation(value = "删除用户关注持卡人员", notes = "删除用户关注持卡人员", response = String.class)
    @PostMapping("/delete")
    public String delete(@Validated({Delete.class}) @RequestBody UserEmployeeRVO userEmployeeRVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("删除用户关注持卡人员: {}", userEmployeeRVO);
        return userEmployeeRService.delete(userEmployeeRVO);
    }
//    @ApiOperation(value = "获取持卡人信息", notes = "获取持卡人信息", response = String.class)
//    @PostMapping("/getEmployeeByUserUid")
//    public String getEmployeeByUserUid(@RequestParam("userUid") String userUid) {
//        return ResultUtil.successWithData(userEmployeeRService.getEmployeeByUserUid(userUid));
//    }
//    @AuthorityVerify
    @OperationLogger(value = "更新持卡人员信息更新")
    @ApiOperation(value = "更新持卡人员信息更新", notes = "更新持卡人员信息更新", response = String.class)
    @PostMapping("/updateEmployeeInfoFormServer")
    public String updateEmployeeInfoFormServer(UserEmployeeRVO userEmployeeRVO) {
        log.info("更新持卡人员信息更新: {}", userEmployeeRVO);
        JSONArray jsonArray = JSONArray.parseArray(userEmployeeRVO.getJsonStr());
        List<ServerSyncEmployeeInfo> resourceList = jsonArray.toJavaList(ServerSyncEmployeeInfo.class);
        return userEmployeeRService.updateEmployeeInfoFormServer(resourceList);
    }

}
